<!-- 证型管理 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入证型名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="openImport()"
          >批量导入</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="证型ID" align="center" prop="id" width="100px" />
      <el-table-column label="证型名称" align="center" prop="name" />
      <el-table-column label="证型简介" align="center" prop="note" />
      <el-table-column label="建议处方" align="center">
        <template slot-scope="{ row }">
          <div v-for="item in row.prescriptList" :key="item.id">
            <span>{{ item.name }}</span>
            <el-tag size="mini" effect="plain" class="ml-5">
              <dict-tag
                :options="dict.type.tcm_prescript_type"
                :value="String(item.prescriptionType)"
              />
            </el-tag>
          </div>
          <div v-for="item in row.regionPrescriptList" :key="item.id">
            <span>{{ item.name }}</span>
            <el-tag size="mini" effect="plain" class="ml-5">
              <dict-tag
                :options="dict.type.tcm_prescript_type"
                :value="String(item.prescriptionType)"
              />
            </el-tag>
            <el-tag
              v-if="item.regionName"
              type="success"
              size="mini"
              effect="plain"
              class="ml-2"
            >
              {{ item.regionName }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOpen(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditTCM ref="EditTCM" @refresh="getList" />

    <ImportModal
      ref="ImportModal"
      r
      title="导入证型"
      url="/tool/importData/tcm/importTypes"
      templateUrl="https://onelive.obs.cn-south-1.myhuaweicloud.com/import-temp/%E8%AF%81%E5%9E%8B%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx"
      @refresh="resetQuery"
      @openErr="openErrorModal"
    />

    <el-dialog
      width="800px"
      title="本次导入数据部分失败，详情请查看失败原因"
      :visible.sync="errorVisible"
      @close="errorVisible = false"
    >
      <el-table :data="errorList">
        <el-table-column label="证型名称" align="center" prop="name" />
        <el-table-column
          label="证型简介"
          align="center"
          prop="note"
          show-overflow-tooltip
        />
        <el-table-column label="导入失败原因" align="center" prop="reason" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getTCMList, deleteTCM } from "@/api/saas/tcm.js";
import EditTCM from './editTCM'
import ImportModal from "@/components/importModal";

export default {
  name: "Tcm",
  dicts: [
    "tcm_prescript_type"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
      },
      // 总条数
      total: 0,
      dataList: [],
      errorVisible: false,
      errorList: []
    };
  },
  components: {
    EditTCM,
    ImportModal
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getTCMList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(row) {
      this.$refs.EditTCM.openModal(row);
    },
    handleDelete(id) {
      this.$modal
        .confirm("是否确认删除该证型吗？")
        .then(function () {
          return deleteTCM(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    openImport() {
      this.$refs.ImportModal.openModal();
    },
    openErrorModal(list) {
      this.errorList = list;
      this.errorVisible = true;
    }
  },
};
</script>
